angular.module('tab-editinfo', [])

.controller 'EditinfoCtrl', ($localStorage,Modal,wfConfig,inWeixin,$location,$scope,$rootScope,Toast,  $state, $ionicHistory,$http,$ionicViewSwitcher, userData,$stateParams) ->
  $scope.editInfo=$localStorage.editInfo
  $scope.info = {}
  $scope.editOrAdd=$localStorage.editOrAdd
  console.log("ifEdit:"+$localStorage.ifEdit)
  if $localStorage.ifEdit==0
    $scope.edit=true
    $scope.add=false
  else
    $scope.edit=false
    $scope.add=true

  #提交修改信息
  $scope.editCommit= ->
    if $localStorage.ifEdit==0
      $http.put('/visitInfo/'+$scope.editInfo.id,{customerName:$scope.editInfo.customerName,tel:$scope.editInfo.tel,bed:$scope.editInfo.bed})
        .success (resp) ->
          Toast.showMsg("修改信息成功")
          $state.go("tab.visinfo")
        .error (resp, status) ->
          console.log(resp)
          Toast.showBusinessError(resp.message)
    else
      $http.post('/visitInfo/',{unitId:$localStorage.currentHos.id,customerName:$scope.info.customerName,tel:$scope.info.tel,bed:$scope.info.bed})
        .success (resp) ->
          if resp.code == 0
            Toast.showMsg("添加信息成功")
            $state.go("tab.visinfo")
          else
            Toast.showMsg(resp.message)
        .error (resp, status) ->
          console.log(resp)
          Toast.showSysError(resp.message)
  #返回
  $scope.back = ->
    window.history.back()
  #删除就诊信息
  $scope.deleteInfo = ->
    $http.delete('/visitInfo/'+$scope.editInfo.id,{})
      .success (resp) ->
        Toast.showMsg("删除信息成功")
        $state.go("tab.visinfo")
      .error (resp, status) ->
        console.log(resp)
        Toast.showBusinessError(resp.message)
  #设为默认
  $scope.setDefault = ->
    $http.put('/visitInfo/'+$scope.editInfo.id,{customerName:$scope.editInfo.customerName,tel:$scope.editInfo.tel,bed:$scope.editInfo.bed,isSelect:1})
      .success (resp) ->
        Toast.showMsg("修改默认成功")
        $state.go("tab.visinfo")
      .error (resp, status) ->
        console.log(resp)
        Toast.showBusinessError(resp.message)
